﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using System.Globalization;

namespace GUI
{
    public partial class frmThietBiNgoaiVi : Form
    {
        public frmThietBiNgoaiVi(int cs)
        {
            InitializeComponent();
            lbCS.Text = cs.ToString();
        }
        private List<int> machsac;
        private List<int> luutru;
        private List<int> kichdien;

        private void btnMachDK_Click(object sender, EventArgs e)
        {
            ChonMach(1);
            CapNhatGiaTuGridView_DK();
        }

        private void btnLuuTru_Click(object sender, EventArgs e)
        {
            ChonMach(2);
            CapNhatGiaTuGridView_LuuTru();
        }

        private void btnKichDien_Click(object sender, EventArgs e)
        {
            ChonMach(3);
            CapNhatGiaTuGridView_KichDien();
        }

        private void ThemVaoGio()
        {
            if (machsac.Count > 0)
            {
                string sql = "select STT,TenTB,MaTB,CSKhaDung,DonGia from MACHSAC where STT=" + machsac[0];
                for (int i = 1; i < machsac.Count; i++)
                {
                    sql+=" or STT="+machsac[i];
                }
                DataTable _dulieu = Provider.TimKiem(sql);
                dgvMachDK.DataSource = _dulieu;
                for (int i = 0; i < dgvMachDK.Rows.Count; i++)
                {
                    int csTB =int.Parse(dgvMachDK.Rows[i].Cells["clCSDK"].Value.ToString());
                    int sl = int.Parse(lbCS.Text) / csTB;
                    if (sl * csTB < int.Parse(lbCS.Text))
                        sl += 1;
                    dgvMachDK.Rows[i].Cells["clSLDK"].Value = sl;
                    dgvMachDK.Rows[i].Cells["clTongCSDK"].Value = sl * csTB;
                    dgvMachDK.Rows[i].Cells["clTongGiaDK"].Value = sl * int.Parse(dgvMachDK.Rows[i].Cells["clGiaDK"].Value.ToString());
                }
                CapNhatGiaTuGridView_DK();
            }
            if (luutru.Count > 0)
            {
                string sql = "select STT,TenTB,MaTB,TenLoai,CSKhaDung,DonGia from AcQuy a,LOAIACQUY b where a.MaLoai=b.MaLoai and (STT=" + luutru[0];
                for (int i = 1; i < luutru.Count; i++)
                {
                    sql += " or STT=" + luutru[i];
                }
                sql += ")";
                DataTable _dulieu = Provider.TimKiem(sql);
                dgvLuuTru.DataSource = _dulieu;
                for (int i = 0; i < dgvLuuTru.Rows.Count; i++)
                {
                    int csTB = int.Parse(dgvLuuTru.Rows[i].Cells["clCSLuuTru"].Value.ToString());
                    int sl = int.Parse(lbCS.Text) / csTB;
                    if (sl * csTB < int.Parse(lbCS.Text))
                        sl += 1;
                    dgvLuuTru.Rows[i].Cells["clSLLuuTru"].Value = sl;
                    dgvLuuTru.Rows[i].Cells["clTongCSLuuTru"].Value = sl * csTB;
                    dgvLuuTru.Rows[i].Cells["clTongGiaLuuTru"].Value = sl * int.Parse(dgvLuuTru.Rows[i].Cells["clGiaLuuTru"].Value.ToString());
                }
                CapNhatGiaTuGridView_LuuTru();
            }
            if (kichdien.Count > 0)
            {
                string sql = "select STT,TenTB,MaTB,TenLoai,CSKhaDung,DonGia from KICHDIEN a,LOAIKICHDIEN b where a.MaLoai=b.MaLoai and (STT=" + kichdien[0];
                for (int i = 1; i < kichdien.Count; i++)
                {
                    sql += " or STT=" + kichdien[i];
                }
                sql += ")";
                DataTable _dulieu = Provider.TimKiem(sql);
                dgvKichDien.DataSource = _dulieu;
                for (int i = 0; i < dgvKichDien.Rows.Count; i++)
                {
                    int csTB = int.Parse(dgvKichDien.Rows[i].Cells["clCSKichDien"].Value.ToString());
                    int sl = int.Parse(lbCS.Text) / csTB;
                    if (sl * csTB < int.Parse(lbCS.Text))
                        sl += 1;
                    dgvKichDien.Rows[i].Cells["clSLKichDien"].Value = sl;
                    dgvKichDien.Rows[i].Cells["clTongCSKichDien"].Value = sl * csTB;
                    dgvKichDien.Rows[i].Cells["clTongGiaKichDien"].Value = sl * int.Parse(dgvKichDien.Rows[i].Cells["clGiaKichDien"].Value.ToString());
                }
                CapNhatGiaTuGridView_KichDien();
            }
        }
        private void ChonMach(int loai)
        {
            frmChonThietBi _frmChon = new frmChonThietBi(loai);
            _frmChon.CsPin = int.Parse(lbCS.Text);
            if(machsac!=null)
                _frmChon.DkSac = machsac;
            if (luutru != null)
                _frmChon.LuuTru = luutru;
            if (kichdien != null)
                _frmChon.KichDien = kichdien;
            _frmChon.ShowDialog();
            machsac = _frmChon.DkSac;
            luutru = _frmChon.LuuTru;
            kichdien = _frmChon.KichDien;
            ThemVaoGio();
        }

        private void dgvMachDK_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dgvMachDK.Columns["clXoaDK"].Index && e.RowIndex >= 0)
            {
                try
                {
                    machsac.Remove(int.Parse(dgvMachDK.CurrentRow.Cells["clSTTDK"].Value.ToString()));
                    dgvMachDK.Rows.Remove(dgvMachDK.CurrentRow);
                    CapNhatGiaTuGridView_DK();
                }
                catch { }
            }
        }

        private void dgvLuuTru_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dgvLuuTru.Columns["clXoaLuuTru"].Index && e.RowIndex >= 0)
            {
                try
                {
                    luutru.Remove(int.Parse(dgvLuuTru.CurrentRow.Cells["clSTTLuuTru"].Value.ToString()));
                    dgvLuuTru.Rows.Remove(dgvLuuTru.CurrentRow);
                    CapNhatGiaTuGridView_LuuTru();
                }
                catch { }
            }
        }

        private void dgvKichDien_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dgvKichDien.Columns["clXoaKichDien"].Index && e.RowIndex >= 0)
            {
                try
                {
                    kichdien.Remove(int.Parse(dgvKichDien.CurrentRow.Cells["clSTTKichDien"].Value.ToString()));
                    dgvKichDien.Rows.Remove(dgvKichDien.CurrentRow);
                    CapNhatGiaTuGridView_KichDien();
                }
                catch { }
            }
        }

        private void CapNhatGiaTuGridView_DK()
        {
            int tong = 0;
            for (int i = 0; i < dgvMachDK.Rows.Count; i++)
            {
                tong += int.Parse(dgvMachDK.Rows[i].Cells["clTongGiaDK"].Value.ToString());
            }
            lbGiaDK.Text = tong.ToString();
        }
        private void CapNhatGiaTuGridView_LuuTru()
        {
            int tong = 0;
            for (int i = 0; i < dgvLuuTru.Rows.Count; i++)
            {
                tong += int.Parse(dgvLuuTru.Rows[i].Cells["clTongGiaLuuTru"].Value.ToString());
            }
            lbGiaLuuTru.Text = tong.ToString();
        }
        private void CapNhatGiaTuGridView_KichDien()
        {
            int tong = 0;
            for (int i = 0; i < dgvKichDien.Rows.Count; i++)
            {
                tong += int.Parse(dgvKichDien.Rows[i].Cells["clTongGiaKichDien"].Value.ToString());
            }
            lbGiaKichDien.Text = tong.ToString();
        }

        private void lbGia_TextChanged(object sender, EventArgs e)
        {
            long tong = int.Parse(lbGiaDK.Text) + int.Parse(lbGiaKichDien.Text) + int.Parse(lbGiaLuuTru.Text);
            lbTongTien.Text = tong.ToString("###,###,##0.", CultureInfo.InvariantCulture).TrimStart().Replace(".", ",");
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmThietBiNgoaiVi_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

    }
}
